#!/usr/bin/python
# -*- mode:python; coding:utf-8 -*-

# find robust features in the image

# NOTE: see https://code.ros.org/trac/opencv/ticket/373 if get
#  "cv.error: Non-positive width or height"

import cv

c = cv.CreateCameraCapture(0)

cv.NamedWindow("Test 06")
cv.StartWindowThread()

while True:
    f = cv.QueryFrame(c)
    if not f: break

    gf = cv.CreateImage((f.width, f.height), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(f, gf, cv.CV_RGB2GRAY)

    try:
        dts = cv.ExtractSURF(gf, None, cv.CreateMemStorage(), (0, 400, 3, 2))[0]
        if len(dts) == 2:
            p1 = tuple(map(int, dts[0][0]))
            p3 = tuple(map(int, dts[1][0]))
            p2 = (p3[0], p1[1])
            p4 = (p1[0], p3[1])
            cv.PolyLine(f, [[p1, p2, p3, p4]], 1, 100, 2)

    except cv.error:
        pass

    cv.ShowImage("Test 06", f)
    d = 1000 / 20
    k = cv.WaitKey(d)
    if k != -1: break


